﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinkSql;
using hospital_registration_system.Entity;
using System.Data;
using MySql.Data.MySqlClient;

namespace hospital_registration_system.DAL
{
    public class Clinic_timeDao
    {
        private string tableName = "clinic_time";
        private string id= "id";//id号
        private string doctor_id = "doctor_id";//医生id
        private string see_time = "see_time";//就诊时间段
        private string treatment = "treatment";//就诊日期
        private string CREATED_BY = "CREATED_BY";//创建人
        private string CREATED_TIME = "CREATED_TIME";//创建时间
        private string UPDATED_BY = "UPDATED_BY";//更新人
        private string UPDATED_TIME = "UPDATED_TIME";//更新时间
        private string state = "state";//状态

        //增加,通过医生id,就诊日期（周几），就诊时间段
        public bool insert(int doctor_id,DateTime treatment, int see_time)
        {
            Dictionary<string, Object> map = new Dictionary<string, object>();
            map.Add(this.doctor_id, doctor_id);
            map.Add(this.see_time, see_time);
            map.Add(this.treatment, treatment);
            map.Add("CREATED_BY", "Tudo");
            map.Add("CREATED_TIME",new DateTime());
            map.Add("UPDATED_BY", "Tudo");
            map.Add("UPDATED_TIME", new DateTime());
            map.Add("state",0);
            return MySQLUtils.insert(tableName, map);
        }

        /// <summary>
        /// 通过id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool delete(int id)
        {
            return MySQLUtils.delete(tableName, id);
        }

        /// <summary>
        /// 通过id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Clinic_time selectById(int id)
        {
            return  GetClinic_TimesByDataTable(MySQLUtils.selectById(tableName, id))[0];
           

        }


        /// <summary>
        /// 通过map查询查询一条记录
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public Clinic_time selectOneByMap(Dictionary<string, Object> map)
        {
            return GetClinic_TimesByDataTable(MySQLUtils.selectByMap(tableName, map))[0];
            
        }

        /// <summary>
        /// 通过map查询出list集合
        /// </summary>
        public List<Clinic_time> selectListByMap(Dictionary<string, Object> map)
        {
           return GetClinic_TimesByDataTable(MySQLUtils.selectByMap(tableName, map));//调用工具类
            
        }

        /// <summary>
        /// 获取所有对象
        /// </summary>
        /// <returns>List集合</returns>
        public List<Clinic_time> selectAll()
        {
            Dictionary<string, Object> map = new Dictionary<string, object>();
            map.Add("1", "1");
            return this.selectListByMap(map);
        }


        /// <summary>
        /// 通过对象修改,建议使用map修改
        /// </summary>
        public bool updateByObject(Clinic_time clinic)
        {
            Dictionary<string, Object> map = new Dictionary<string, object>();
            map.Add(this.doctor_id, clinic.Doctor_id);
            map.Add(this.see_time, clinic.See_time);
            map.Add(this.treatment, clinic.Treatment);
            map.Add(this.UPDATED_BY, clinic.UPDATED_BY1);
            map.Add(this.UPDATED_TIME, clinic.UPDATED_TIME1);
            map.Add(this.state, clinic.State);
            return MySQLUtils.update(tableName, map);
        }

        /// <summary>
        /// 通过map修改，key：修改的字段名，value:修改后的值
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public bool updateByMap(Dictionary<string, Object> map)
        {
            if (map["id"] == null || "".Equals(map["id"])) return false;
            return MySQLUtils.update(tableName, map);
        }


        /// <summary>
        /// 将DataTable转化为List<Clinic_time>
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public List<Clinic_time> GetClinic_TimesByDataTable(DataTable dataTable)
        {
            List<Clinic_time> ret = new List<Clinic_time>();//创建集合
            foreach (DataRow row in dataTable.Rows)
            {
                ret.Add(new Clinic_time(//对象放入集合中

                   int.Parse(row["id"].ToString()),
                   int.Parse(row["doctor_id"].ToString()),
                   int.Parse(row["see_time"].ToString()),
                   (DateTime)(row["treatment_date"]),
                   row["CREATED_BY"].ToString(),
                   (DateTime)(row["CREATED_TIME"]),
                   row["UPDATED_BY"].ToString(),
                   (DateTime)(row["UPDATED_TIME"]),
                   int.Parse(row["state"].ToString())

                ));
            }
            return ret;
        }

        /// <summary>
        /// 获取就诊时段
        /// </summary>
        /// <param name="time">就诊日期</param>
        /// <param name="keshi_id">科室</param>
        /// <returns></returns>
        public List<Clinic_time> GetClinic_Times(DateTime time,int keshi_id)
        {
            //根据科室id查询出医生信息
            DoctorDao dao = new DoctorDao();
            Dictionary<string, Object> doctorret = new Dictionary<string, object>();
            doctorret.Add("department_id", keshi_id);
            List<Doctor> doctors =dao.selectByMap(doctorret);

            List<Clinic_time> ret = new List<Clinic_time>();
            //查出所有的时段信息
            List<Clinic_time> list = GetClinic_TimesByDataTable(MySQLUtils.selectAll(tableName));//通过DateTime获取就诊时段
            foreach(Clinic_time clinic in list)
            {
                //首先判断时间是否符合
                if(time.Year == clinic.Treatment.Year && time.Month==clinic.Treatment.Month&& time.Day == clinic.Treatment.Day)
                {
                    //再查找符合科室要求的预约时段
                    foreach(Doctor d in doctors)
                    {
                        if(d.Id == clinic.Doctor_id)
                        {
                            ret.Add(clinic);
                        }
                    }
                }
            }
            return ret;
        } 

        /// <summary>
        /// 该时段预约数+1
        /// </summary>
        /// <param name="cId"></param>
        /// <returns></returns>
        public bool countAdd(int cId)
        {
            string sql = "update clinic_time set state =state+1 where id = " + cId;
            MySqlConnection connection = null;
            try
            {
                connection = MySQLUtils.GetMySQLConnection();
                connection.Open();
                MySqlCommand cmd = new MySqlCommand(sql, connection);
                cmd.ExecuteNonQuery();
            }
            catch (MySqlException)
            {
                return false;
            }
            finally
            {
                connection.Close();
            }
            return true;
        }

        /// <summary>
        /// 该时段预约数-1
        /// </summary>
        /// <param name="cId"></param>
        /// <returns></returns>
        public bool countSub(int cId)
        {
            string sql = "update clinic_time set state =state-1 where id = " + cId;
            MySqlConnection connection = null;
            try
            {
                connection = MySQLUtils.GetMySQLConnection();
                connection.Open();
                MySqlCommand cmd = new MySqlCommand(sql, connection);
                cmd.ExecuteNonQuery();
            }
            catch (MySqlException)
            {
                return false;
            }
            finally
            {
                connection.Close();
            }
            return true;
        }

    }
}
